<?php
namespace Admin\Controller;
use Think\Controller;

/**
 * 后台上传文件管理接口
 *
 */
class AttController extends Controller
{
    public function index()
    {
        $res = array('rcode'=>1,'data'=>date('Y-m-d H:i:s'),'msg'=>'OK');
        echo json_encode( $res );
        exit();
    }

    //前端上传文件接口
    public function upload()
    {
        if (IS_GET) {
            //
        }
        else{
            $res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'Error');
            $fname = I('post.name','file', 'trim');
            $newName = I('post.newname','','trim');
            $from = I('post.from','', 'trim');
            $Mdo = new \Common\ORG\AttUpload();
            if ( $_FILES && isset( $_FILES[$fname] ) ){
                $fupload = $_FILES[$fname];
                $is_multi = is_array($fupload['error'])  ? true : false;
                $res['f'] = $is_multi;
                if ( (!$is_multi&&$fupload['error'] != 0) || ($is_multi&&count($fupload['error'])==0)  ||
                         ($is_multi && $fupload['error'][0]!=0 )){
                    $res['msg'] = "上传文件发生错误";
                }
                else {
                    if ($is_multi) {  // 二维数组批量上传文件
                        $res = array('rcode'=>0,'data'=>array(),'msg'=>'Error');
                        $allFileSize = count($fupload['name']);
                        $countUploaded = 0;
                        for ($i=0;  $i<$allFileSize; $i++) {
                            $ret = $Mdo->upload( $fupload['name'][$i], $fupload['tmp_name'][$i], $newName);
                            if ($ret&&$ret['rcode']){
                                $countUploaded++;
                                $res['data'][] = $ret['data'];
                            } 
                            
                        }
                        if ($countUploaded) {
                            $res['rcode'] = 1;
                            $res['msg'] = "OK";
                        }
                        else 
                            $res['msg'] = "批量上传文件失败";
                    }
                    else {
                        $res = $Mdo->upload( $fupload['name'], $fupload['tmp_name'], $newName);
                    }
                }

            }
            else
                $res['msg'] = "获取上传文件失败";
            if ($from && $from =='editor') {
                if ($res['rcode']) {
                    echo $res['data']['view_path'];
                }
                else
                    echo "error|",$res['msg'];
                exit();
            }
            e_json( $res );
        }
    }

    //解析文件接口
    public function parse_excel()
    {
        $res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'Error');
        if( IS_POST ){
            $path = I('post.path','','trim');
            if( !$path )
                $res['msg'] = L('PARAM_WRONG');
            else{
                $Mdo = new \Common\ORG\Excel();
                $parseRes = $Mdo->parse_file( $path );
                $res = $parseRes;
            }
        }

        e_json( $res );
    }
}

?>
